//
//  LXTreeView.m
//  SourceCodeRead
//
//  Created by Kael on 2018/5/23.
//  Copyright © 2018年 Kael. All rights reserved.
//

#import "LXTreeView.h"
#import "Masonry.h"

@interface LXTreeView()<UITableViewDelegate,UITableViewDataSource>

@end


@implementation LXTreeView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initBaseData];
        [self initBaseView];
    }
    return self;
}

#pragma mark - **************** 初始化操作
- (void)initBaseData {
    
}

- (void)initBaseView {
    __weak typeof(&*self)weakSelf = self;
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(weakSelf);
    }];
}

#pragma mark lazy loading...
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

#pragma tableView--delegate
#pragma tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *item = _dataSource[section];
    NSArray *list = [item objectForKey:@"childs"];
    return list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identify = @"cellIdentify";
    LXTreeCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[LXTreeCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
    }
    NSDictionary *item = _dataSource[indexPath.section];
    NSArray *list = [item objectForKey:@"childs"];
    NSDictionary *cellItem =list[indexPath.row];
    
    NSString *title = [cellItem objectForKey:@"title"];
    NSString *level  = [cellItem objectForKey:@"level"];

    cell.textLabel.text = [NSString stringWithFormat:@"%@%@",title,level];
    [cell loadData:cellItem];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, self.frame.size.width, 44);
    
    UILabel *firstLevalHeder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
    firstLevalHeder.backgroundColor = [UIColor lightGrayColor];
    NSString *title = [[_dataSource objectAtIndex:section] objectForKey:@"title"];
    NSString *level  = [[_dataSource objectAtIndex:section] objectForKey:@"level"];
    firstLevalHeder.text = [NSString stringWithFormat:@"%@%@",title,level];
    
    [headerView addSubview:firstLevalHeder];
    
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return  144;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}



@end
